//
//  MapGeocode.m
//  carcareIOS
//
//  Created by ileo on 16/8/10.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MapGeocode.h"


@interface MapGeocodeRequestInstance : NSObject <AMapSearchDelegate>

@property (nonatomic, strong) AMapSearchAPI *search;

@property (nonatomic, copy) void (^Error)();
@property (nonatomic, copy) void (^ReGeocode)(AMapReGeocode *reGeocode);
@property (nonatomic, copy) void (^Geocode)(NSArray<AMapGeocode *> *geocodes);

@end

@implementation MapGeocodeRequestInstance

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.search = [[AMapSearchAPI alloc] init];
        self.search.delegate = self;
    }
    return self;
}

-(void)reGeoCodeCoordinate:(CLLocationCoordinate2D)coordinate finish:(void(^)(AMapReGeocode *reGeocode))finish error:(void(^)())error{
    self.ReGeocode = finish;
    self.Error = error;
    AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
    request.radius = 250;
    request.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    request.requireExtension = YES;
    [self.search AMapReGoecodeSearch:request];
}

-(void)geoCodeAddress:(NSString *)address finish:(void(^)(NSArray<AMapGeocode *> *geocodes))finish error:(void(^)())error{
    self.Geocode = finish;
    self.Error = error;
    AMapGeocodeSearchRequest *request = [[AMapGeocodeSearchRequest alloc] init];
    request.address = address;
    [self.search AMapGeocodeSearch:request];
}

#pragma mark - search
-(void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
    DLog(@"map search request fail : %@",error);
    if (self.Error) self.Error();
}

-(void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
    if (self.ReGeocode) self.ReGeocode(response.regeocode);
}

-(void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response{
    if (self.Geocode) self.Geocode(response.geocodes);
}

@end



#pragma mark - MapGeocode

@implementation MapGeocode

static NSArray *requests;

+(MapGeocodeRequestInstance *)newRequest{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:requests];
    MapGeocodeRequestInstance *request = [[MapGeocodeRequestInstance alloc] init];
    [arr addObject:request];
    requests = [arr copy];
    return request;
}

+(void)removeRequest:(MapGeocodeRequestInstance *)request{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:requests];
    [arr removeObject:request];
    requests = [arr copy];
}

+(void)reGeoCodeCoordinate:(CLLocationCoordinate2D)coordinate finish:(void(^)(AMapReGeocode *reGeocode))finish error:(void(^)())error{
    MapGeocodeRequestInstance *instance = [MapGeocode newRequest];
    __weak __typeof(instance) wInstance = instance;
    [instance reGeoCodeCoordinate:coordinate finish:^(AMapReGeocode *reGeocode) {
        [MapGeocode removeRequest:wInstance];
        if (finish) finish(reGeocode);
    } error:^{
        [MapGeocode removeRequest:wInstance];
        if (error) error();
    }];
}

+(void)geoCodeAddress:(NSString *)address finish:(void(^)(NSArray<AMapGeocode *> *geocodes))finish error:(void(^)())error{
    MapGeocodeRequestInstance *instance = [MapGeocode newRequest];
    __weak __typeof(instance) wInstance = instance;
    [instance geoCodeAddress:address finish:^(NSArray<AMapGeocode *> *geocodes) {
        [MapGeocode removeRequest:wInstance];
        if (finish) finish(geocodes);
    } error:^{
        [MapGeocode removeRequest:wInstance];
        if (error) error();
    }];
}

@end
